<template>
  <div class="registerView">
    <div class="registerBox">
      <h2>注册页面</h2>
      <el-form ref="formRef" :model="regForm" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="regForm.username" placeholder="用户名" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="regForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="regForm.confirmPassword"
            type="password"
            placeholder="确认密码"
            show-password
          />
        </el-form-item>
      </el-form>
      <div class="registerBtn">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <router-link to="/login">
          <el-button type="primary" @click="resetForm">返回登录</el-button>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const regForm = ref({
  username: '',
  password: '',
  confirmPassword: '',
})
const formRef = ref(null)

// 自定义验证规则函数
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请确认新密码'))
  } else if (value !== regForm.value.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度为6-16位', trigger: 'blur' },
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度为6-16位', trigger: 'blur' },
    { validator: validatePass2, trigger: 'blur' },
  ],
}

const submitForm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      const userObj = {
        username: regForm.value.username,
        password: regForm.value.password,
      }
      axios
        .post('http://localhost:3001/register', userObj)
        .then(() => {
          ElMessage.success('注册成功')
          router.push('/login')
        })
        .catch((error) => {
          const message = error.response?.data?.message || error.message
          ElMessage.error('注册失败: ' + message)
        })
    } else {
      ElMessage.error('表单填写不正确')
    }
  })
}

// 返回登陆后,重置表单
const resetForm = () => {
  regForm.value = {
    username: '',
    password: '',
    confirmPassword: '',
  }
  if (formRef.value) {
    formRef.value.resetFields()
  }
}
</script>

<style lang="scss" scoped>
.registerView {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('images/bg.png');
  background-size: cover;

  .registerBox {
    display: flex;
    background-color: #fff;
    padding: 20px;
    margin: auto;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
    border-radius: 10px;
    flex-direction: column;
    align-items: center;
    gap: 20px;

    .el-form-item {
      width: 240px;

      .el-input {
        width: 100%;
      }
    }

    .registerBtn {
      display: flex;
      gap: 20px;
      justify-content: center;
      align-items: center;

      .el-button {
        width: 70px;
        height: 35px;
      }
    }
  }
}
</style>
